﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.AcmsControls.ContentControls.FrontEnd;
using Acms.Bll;

namespace Ait.Web.Templates.SectionOverview
{
    public partial class _default : Acms.AcmsControls.TemplatePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            dsCaseStudies.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsCaseStudies_SelectData);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Quote.IsEmpty)
                Quote.Visible = false;
            if (QuoteRef.IsEmpty)
                QuoteRef.Visible = false;

            if (Quote.IsEmpty && QuoteRef.IsEmpty)
            {
                phQuote.Visible = false;
            }
        }


        protected void dsCaseStudies_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
        {
           
            
            var caseStudies = new List<Post>();
            if (AcmsContext.CurrentPost != null)
            {
                GetCaseStudies(AcmsContext.CurrentPost.Children, caseStudies);
            }
            
            e.SetData(caseStudies);
        }

        private void GetCaseStudies(IEnumerable<Post> posts, List<Post> caseStudies)
        {
            if (posts == null)                
                return;
            
            foreach (var post in posts)
            {
                if (post.TemplateName == "CaseStudiesItem")
                {
                    caseStudies.Add(post);
                    phCaseStudies.Visible = true;
                }
            }
            foreach (var post in posts)
            {
                GetCaseStudies(post.Children, caseStudies);
            }
        }

    }
}
